import { societyIdentity, vipLevels, monthEventFormat } from '@/common/constant/society.js'
import { navigate } from "@/common/util/uni";

// 刷新前一个页面
// [funcName]：前一个页面的方法
// [data]：需要传递给前一个页面方法的参数
export const refreshPrePage = (funcName, data='') => {
  const pages = getCurrentPages()
  const prePage = pages[pages.length -2]
  if (prePage.$vm[funcName]) {
    prePage.$vm[funcName](data)
  }
}

// 是否是图片
export const isImageUrl = (url) => /\.(xbm|tif|pjp|svgz|jpg|jpeg|ico|tiff|gif|svg|jfif|webp|png|bmp|pjpeg|avif)/.test(url)

// 格式化学社标签
export function formatIdentity (member_position) {
  return societyIdentity.find(v => v.value == member_position)?.label
}

// 格式化vip标签
export function formatVipIcon (vip_level) {
  return vipLevels.find(v => v.value == vip_level)?.icon
}

// 格式化每月必做事件
export function onthEventFilter (value, type) {
  return monthEventFormat.find(v => v.value == value)[type]
}

// 跳转个人主页
export function toHomePage (user_id) {
  navigate(`/package_user/pages/mine/index?user_id=${user_id}`)
}

// 文件下载
export function downloadFile(url, loadingText='正在下载') {
  return new Promise((resolve, reject) => {
    uni.showLoading({
      title: loadingText,
    })
    uni.downloadFile({
      url,
      success: res => resolve(res.tempFilePath),
      fail: err => reject(err),
      complete: () => uni.hideLoading()
    })
  })
}